//
//  SISSConfirmPurchaseIntentionViewController.m
//  SISSAPP
//
//  Created by 袁应荣 on 2019/7/5.
//  Copyright © 2019 优房网. All rights reserved.
//

#import "SISSConfirmIntentionViewController.h"
#import "AttributedLabel.h"
#import "SISSImportantHouseTakeLookRecordCell.h"
#import "SISSImportantPassengerTakeLookRecordCell.h"


@interface SISSConfirmIntentionViewController ()

///
@property (weak, nonatomic) UIView *tipView;
@property (weak, nonatomic) AttributedLabel *tipLabel;

/* confirmButton */
@property (weak, nonatomic) UIButton *confirmButton;

@end

@implementation SISSConfirmIntentionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}


#pragma mark - 点击确定按钮
- (void)confirmButtonDidClick:(UIButton *)button {
    
    [self confirmButtonDidClickAction];
}


- (void)confirmButtonDidClickAction {
    
}


- (void)setIsSelectMain:(BOOL)isSelectMain {
    _isSelectMain = isSelectMain;
    
    UIColor *bgColor = isSelectMain ? MAIN_COLOR : [UIColor groupTableViewBackgroundColor];
    [self.confirmButton setBackgroundColor:bgColor];
    [self.confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.confirmButton.userInteractionEnabled = isSelectMain;
}

- (void)setHasConfirm:(BOOL)hasConfirm {
    _hasConfirm = hasConfirm;
    
    [self.confirmButton setTitle:hasConfirm ? @"取消交易" : @"确定" forState:UIControlStateNormal];
    [self.confirmButton setBackgroundColor:hasConfirm ? MAIN_COLOR : [UIColor groupTableViewBackgroundColor]];
    self.confirmButton.userInteractionEnabled = hasConfirm;
    self.confirmButton.hidden = hasConfirm;
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseID = @"reuseID";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if ( !cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseID];
    }
    return cell;
}




#pragma mark - UI
- (void)setupSubviews {
    [super setupSubviews];
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UIView *tipView = [[UIView alloc] init];
    tipView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:tipView];
    self.tipView = tipView;
    
    AttributedLabel *tipLabel = [AttributedLabel yyr_labelWithText:@"温馨提示：\n仅“已确认”的有效看房记录可发起交易。" fontSize:16 textColor:SECONDARY_TEXT_COLOR];
    [tipLabel setFont:[UIFont boldSystemFontOfSize:16] fontColor:MAIN_TEXT_COLOR ofRange:NSMakeRange(0, 5)];
    [tipLabel setLineSpace:4 ofRange:NSMakeRange(0, tipLabel.text.length)];
    [self.tipView addSubview:tipLabel];
    self.tipLabel = tipLabel;
    
    SISSBaseTableView *tableView = [[SISSBaseTableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.dataSource = self;
    tableView.delegate = self;
    // 自动行高
    tableView.estimatedRowHeight = 80;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [tableView registerClass:[SISSImportantHouseTakeLookRecordCell class] forCellReuseIdentifier:importantHouseTakeLookRecordCellReuseID];
    [tableView registerClass:[SISSImportantPassengerTakeLookRecordCell class] forCellReuseIdentifier:importantPassengerTakeLookRecordCellReuseID];
    [self.view addSubview:tableView];
    self.tableView = tableView;
    
    // 确定
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:self.hasConfirm ? @"取消交易" : @"确定" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:self.hasConfirm ? MAIN_COLOR : [UIColor darkGrayColor]];
    confirmButton.userInteractionEnabled = self.hasConfirm;
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmButton addTarget:self action:@selector(confirmButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
    confirmButton.layer.cornerRadius = 4;
    confirmButton.layer.masksToBounds = YES;
    confirmButton.hidden = self.isHistory ? YES : self.hasConfirm;
    [self.view addSubview:confirmButton];
    self.confirmButton = confirmButton;
    
}

#pragma mark - 约束
- (void)initConstraint {
    [super initConstraint];
    
    // 自动布局
    CGFloat margin = 10;
    CGSize buttonSize = CGSizeMake(SCREEN_WIDTH * 0.8, 54);
    WeakSelf
    [self.tipView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.mas_topLayoutGuideBottom);
        make.left.right.equalTo(weakSelf.view);
    }];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tipView).offset(margin);
        make.left.equalTo(weakSelf.tipView).offset(2 * globelMargin);
        make.bottom.equalTo(weakSelf.tipView).offset(-margin);
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tipView.mas_bottom);
        make.left.right.equalTo(weakSelf.view);
        make.height.mas_equalTo(100);
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.tableView.mas_bottom).offset(3 * globelMargin);
        make.centerX.equalTo(weakSelf.view);
        make.size.mas_equalTo(buttonSize);
//        if (@available(iOS 11.0, *)) {
//            make.bottom.equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
//        } else {
//            make.bottom.equalTo(weakSelf.view);
//        }
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
